<?php
namespace app\index\controller;
use think\Paginator;
use app\common\controller\Homebase;

class Census extends Homebase
{ 
    //接单统计
    public function order(){
        $date = $this->request->param('date');
        $order = db('order');
        $count = db('order');
        $che_count = db('order');
        $zhong_count = db('order');
        $back_count = db('order');
        $able_count = db('order');
        $able_money = db('order');
        $able_paymoney = db('order');
        if($date && is_array(explode('-',$date))){
            $date = explode('-',$date);
            $order->where('create_time','between',[strtotime($date[0]),strtotime($date[1])]);
            $count->where('create_time','between',[strtotime($date[0]),strtotime($date[1])]);
            $che_count->where('create_time','between',[strtotime($date[0]),strtotime($date[1])]);
            $zhong_count->where('create_time','between',[strtotime($date[0]),strtotime($date[1])]);
            $back_count->where('create_time','between',[strtotime($date[0]),strtotime($date[1])]);
            $able_count->where('create_time','between',[strtotime($date[0]),strtotime($date[1])]);
            $able_money->where('create_time','between',[strtotime($date[0]),strtotime($date[1])]);
            $able_paymoney->where('create_time','between',[strtotime($date[0]),strtotime($date[1])]);
        }
        //总数        
        $count = $count->count();
        $this->assign('count',$count);
        //撤单数
        
        $che_count = $che_count->where('status',6)->count();
        $this->assign('che_count',$che_count);  
        //仲裁数
        
        $zhong_count = $zhong_count->where('status',5)->count();
        $this->assign('zhong_count',$zhong_count);
        //退款数
        
        $back_count = $back_count->where('status',7)->count();
        $this->assign('back_count',$back_count);
        //有效数
        
        $able_count = $able_count->where('status','in',[3,4])->count();
        $this->assign('able_count',$able_count);
        //有效金额
        
        $able_money = $able_money->where('status','in',[3,4])->sum('price');
        $this->assign('able_money',$able_money);
        //有效支出金额
        
        $able_paymoney = $able_paymoney->where('status','in',[3,4])->sum('orderpeice');
        $this->assign('able_paymoney',$able_paymoney);
        return $this->fetch();
    }

    //客户统计
    public function customer(){
        $data = $this->request->param();
        $user = db('order');
        $count = db('order');
        $query = [];
        if(isset($data['date']) && $data['date']){
            $date = explode('-',$data['date']);
            $user->where('create_time','between',[strtotime($date[0]),strtotime($date[1])]);
            $count->where('create_time','between',[strtotime($date[0]),strtotime($date[1])]);
            $query['date'] = $data['date'];
        }
        if(isset($data['iphone']) && $data['iphone']){
            $user->where('iphone','like','%'.$data['iphone'].'%');
            $count->where('iphone','like','%'.$data['iphone'].'%');
            $query['iphone'] = $data['iphone'];
        }
        if(isset($data['taobao']) && $data['taobao']){
            $user->where('taobao','like','%'.$data['taobao'].'%');
            $count->where('taobao','like','%'.$data['taobao'].'%');
            $query['taobao'] = $data['taobao'];
        }
        if(isset($data['qq']) && $data['qq']){
            $user->where('qq','like','%'.$data['qq'].'%');
            $count->where('qq','like','%'.$data['qq'].'%');
            $query['qq'] = $data['qq'];
        }
        $count = $count->count();
        $user = $user->paginate(10,$count,['query'=>$query]);
        $this->assign('user',$user);
        return $this->fetch();
    }

    public function msg(){
        return $this->fetch();
    }
}
